//
//  HMTimingManager.m
//  项目的登录注册模块的封装
//
//  Created by 孙鹏飞 on 2017/3/20.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMTimingManager.h"

@interface HMTimingManager ()

@property(nonatomic,assign)NSInteger  times;
@property(nonatomic,strong)NSTimer * myTimer;
@property(nonatomic,copy) void (^getCurrentTimesBlock)(NSInteger times);

@end

@implementation HMTimingManager

NSString * const RIVUserTimingFinishNotification = @"RIVUserTimingFinishNotification"; //定时结束通知

static  HMTimingManager *_instance;

+ (id)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

+ (instancetype)sharedTool {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[HMTimingManager alloc] init];
    });
    return _instance;
}

- (id)init
{
    self = [super init];
    if (self) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            // 对象属性的初始化
            _times = 0;
        });
    }
    return self;
}
- (void)addTimer
{
    if(self.myTimer) return;
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFuc) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.myTimer forMode:NSRunLoopCommonModes];
}
-(void)stopTimer
{
    [self.myTimer invalidate];
    self.myTimer = nil;
    _times = 0;
    self.selectedIndexRow = 0;
    
}
- (void)timerFuc {
    if (--_times <= 0) {
        [self stopTimer];
        //计时结束，发送结束通知。
        [[NSNotificationCenter defaultCenter] postNotificationName:RIVUserTimingFinishNotification object:nil];
    }
    self.getCurrentTimesBlock(_times);
}
- (void)initTimer:(NSInteger)second{
    _times = second;
    [self addTimer];
}
- (NSInteger)getCurrentTimes {
    return _times;
}
- (BOOL)isTiming {
    return self.myTimer ? YES : NO;
}

- (void)getCurrentTimesBlock:(void(^)(NSInteger result))times {
    self.getCurrentTimesBlock = times;
}

@end

static HMCodeTimingManager *_codeInstance;
@interface HMCodeTimingManager()
@property(nonatomic,assign)NSInteger codeTime;
@property(nonatomic,strong)NSTimer * codeTimer;
@property(nonatomic,copy) void (^getCurrentTimesBlock)(NSInteger times);
@end

@implementation HMCodeTimingManager

+ (instancetype)sharedTool {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _codeInstance = [[HMCodeTimingManager alloc] init];
    });
    return _codeInstance;
}

- (id)init
{
    self = [super init];
    if (self) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            // 对象属性的初始化
            _codeTime = 0;
        });
    }
    return self;
}

- (void)addTimer
{
    if(self.codeTimer) return;
    self.codeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFuc) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.codeTimer forMode:NSRunLoopCommonModes];
}
-(void)stopTimer
{
    [self.codeTimer invalidate];
    self.codeTimer = nil;
    _codeTime = 0;
}
- (void)timerFuc {
    if (--_codeTime <= 0) {
        [self stopTimer];
        //计时结束，发送结束通知。
        [[NSNotificationCenter defaultCenter] postNotificationName:RIVUserTimingFinishNotification object:nil];
    }
    self.getCurrentTimesBlock(_codeTime);
}
- (void)initTimer:(NSInteger)second{
    _codeTime = second;
    [self addTimer];
}
- (NSInteger)getCurrentTimes {
    return _codeTime;
}
- (BOOL)isTiming {
    return self.codeTimer ? YES : NO;
}

- (void)getCurrentTimesBlock:(void(^)(NSInteger result))times {
    self.getCurrentTimesBlock = times;
}



@end
